
datablock DebrisData(Armor_RicochetDebris) {
	shapeFile = "Add-Ons/Weapon_Gun/gunshell.dts";
	lifetime = 5.0;
	minSpinSpeed = -2000.0;
	maxSpinSpeed = 2000.0;
	elasticity = 0.2;
	friction = 0.8;
	numBounces = 2;
	staticOnMaxBounce = true;
	snapOnMaxBounce = false;
	fade = true;
	gravModifier = 2;
};
datablock ExplosionData(Armor_RicochetExplosion) {
	debris = Armor_RicochetDebris;
	debrisNum = 1;
	debrisNumVariance = 0;
	debrisPhiMin = 0;
	debrisPhiMax = 0;
	debrisThetaMin = 0;
	debrisThetaMax = 0;
	debrisVelocity = 10;
	debrisVelocityVariance = 5;
};
datablock ProjectileData(Armor_RicochetProjectile) {
	explosion = Armor_RicochetExplosion;
	explodeOnDeath = 1;
	lifetime = 10;
};

datablock AudioProfile(Armor_RicochetSound1) {
	filename = "./Content/bulletimpact1.wav";
	description = AudioClose3d;
	preload = true;
};
datablock AudioProfile(Armor_RicochetSound2) {
	filename = "./Content/bulletimpact2.wav";
	description = AudioClose3d;
	preload = true;
};
datablock AudioProfile(Armor_RicochetSound3) {
	filename = "./Content/bulletimpact3.wav";
	description = AudioClose3d;
	preload = true;
};

function VectorReflect(%vec, %norm) {
	%norm = VectorNormalize(%norm);
	return vectorSub(%vec, VectorScale(%norm, 2*VectorDot(%vec, %norm)));
}
function VectorAngle(%a, %b) {
	%a = VectorNormalize(%a);
	%b = VectorNormalize(%b);
	return mAcos(VectorDot(%a, %b)) / $pi * 180;
}

if(!isFunction(ProjectileData, onCollision)) eval("function ProjectileData::onCollision(){}");

package RedBase_Armor {
	function Armor::damage(%armor, %target, %source, %position, %damage, %damageType) {
		if(%damage < %armor.armorMinimumDamage) return;
		parent::damage(%armor, %target, %source, %position, %damage, %damageType);
	}
	function WheeledVehicleData::damage(%armor, %target, %source, %position, %damage, %damageType) {
		if(%damage < %armor.armorMinimumDamage) return;
		parent::damage(%armor, %target, %source, %position, %damage, %damageType);
	}
	function FlyingVehicleData::damage(%armor, %target, %source, %position, %damage, %damageType) {
		if(%damage < %armor.armorMinimumDamage) return;
		parent::damage(%armor, %target, %source, %position, %damage, %damageType);
	}
	
	// Ricochet
	function ProjectileData::onCollision(%projdata, %proj, %target, %fade, %pos, %normal, %velocity) {
		%projdamage = %projdata.directDamage;
		if(isObject(%projdata.explosion)) { %projdamage += %projdata.explosion.radiusDamage; }
		if(
			isObject(%target) &&
			(%target.getType() & ($TypeMasks::PlayerObjectType | $TypeMasks::VehicleObjectType)) &&
			%projdamage < %target.getDatablock().armorMinimumDamage
		) {
			if(
				!%proj.Armor_isRicochet &&
				%projdata.radiusDamage==0 &&
				(%projdata.bounceAngle == 0 || %projdata.bounceElasticity < 0.1) &&
				(!isObject(%projdata.explosion) || %projdata.explosion.radiusDamage<10)
			) {
				%vel = %proj.getVelocity();
				%vel2 = VectorReflect(%vel, %normal);
				%vel2 = VectorScale(%vel2, 0.5);
				
				%rpdata = %projdata.bounceDestroyProjectile;
				//if(%rpdata $= "") { %rpdata = Armor_RicochetProjectile; }
				if(%rpdata $= "") { %rpdata = %projdata; %delete = 1; }
				%proj2 = new Projectile() {
					dataBlock = %rpdata;
					initialPosition = %pos;
					initialVelocity = %vel2;
					sourceObject = %proj.sourceObject;
					client = %proj.client;
					sourceSlot = 0;
					originPoint = %pos;
					Armor_isRicochet = true;
				};
				MissionCleanup.add(%proj2);
				%proj2.setScale(%proj.getScale());
				if(%delete) { %proj2.schedule(50, delete); }
				
				serverPlay3D("Armor_RicochetSound" @ getRandom(1, 3), %pos);
			}
			//if(%projdata.destroyOnBounce || %projdata.deleteOnBounce) {
			//	%proj.delete();
			//} else {
			//	%proj.explode();
			//}
			%proj.delete();
		} else {
			parent::onCollision(%projdata, %proj, %target, %fade, %pos, %normal, %velocity);
		}
	}
};
activatePackage(RedBase_Armor);
